$(function(){
    var orderBillId = $("[id=id]").val();
    $.post("/sourceClient/searchApplyClient?id="+orderBillId,function(data){
        $("#form_applyClient").form("load",data);
    },"json");
    $.post("/sourceClient/searchRecognizee?id="+orderBillId,function(data){
        $("#form_recognizee").form("load",data);
    },"json");
    $.post("/sourceClient/searchCarOwner?id="+orderBillId,function(data){
        $("#form_carOwner").form("load",data);
    },"json");
    $.post("/orderBill/selectOne?id="+orderBillId,function(data){

        console.log(data);
        data["accountInfo.id"]=data.accountInfo.id;
        data["accountName"]=data.accountInfo.accountName;
        data["accountNumber"]=data.accountInfo.accountNumber;
        data["bankProvince"]=data.accountInfo.bankProvince;
        data["bankCity"]=data.accountInfo.bankCity;
        data["bankName"]=data.accountInfo.bankName;
        data["subbankName"]=data.accountInfo.subbankName;
        //承保信息表单
        data["organizationId"]=data.organization.id;
        data["productId"]=data.product.id;
        //车库汽车信息
        data["carInfo.id"]=data.carInfo.id;
        data["sn"]=data.carInfo.sn;
        data["carType"]=data.carInfo.carType;
        data["basesNumber"]=data.carInfo.basesNumber;
        data["airRelease"]=data.carInfo.airRelease;
        data["seatNumber"]=data.carInfo.seatNumber;
        data["rateDuality"]=data.carInfo.rateDuality;
        data["mainChassis"]=data.carInfo.mainChassis;
        data["engineNumber"]=data.carInfo.engineNumber;
        data["newCarPrice"]=data.carInfo.newCarPrice;
        data["importation"]=data.carInfo.importation;
        data["power"]=data.carInfo.power;
        //客户车辆信息
        data["clientCarInfo.id"]=data.clientCarInfo.id;
        data["hasCarNumber"]=data.clientCarInfo.hasCarNumber;
        data["newCar"]=data.clientCarInfo.newCar;
        data["foreignCar"]=data.clientCarInfo.foreignCar;
        data["transferCar"]=data.clientCarInfo.transferCar;
        data["carNumberType"]=data.clientCarInfo.carNumberType;
        data["carNumber"]=data.clientCarInfo.carNumber;
        data["registerTime"]=data.clientCarInfo.registerTime;
        data["workingNature"]=data.clientCarInfo.workingNature;
        data["workingArea"]=data.clientCarInfo.workingArea;
        data["transferTime"]=data.clientCarInfo.transferTime;
        data["driverInfo"]=data.clientCarInfo.driverInfo;
        data["driverYears"]=data.clientCarInfo.driverYears;
        data["fleet"]=data.clientCarInfo.fleet;
        data["queryNumber"]=data.clientCarInfo.queryNumber;
        data["relationToClient"]=data.clientCarInfo.relationToClient;
        data["checkNumber"]=data.clientCarInfo.checkNumber;
        data["applyTime000"]=data.applyTime;
        data["beginTime000"]=data.beginTime;
        data["endTime000"]=data.endTime;
        data["registerTime000"]=data.registerTime;
        data["transferTime000"]=data.transferTime;
        $("#form_timeandbaseinfo").form("load",data);
        $("#form_organdpro").form("load",data);
        $("#form_clientCarInfo").form("load",data);
        $("#form_accountInfo").form("load",data);
    },"json");
    //*********投保向导页面的事件*************
    $("#organization_id").combobox({
        onChange:function(){
            var id = $("#organization_id").combobox("getValue");

            $("#product_id").combobox({
                onBeforeLoad: function(param){
                    param.orgId = id;
                }
                })
            /*$("#product_id").combobox("reload")*/
        }
    })
    //*********统一定义所有的panel面版*************
    $(".orderBill_panel00").panel({
    })


    //同被保人按钮  选中后将被保人信息填写框 先清除该表单的数据 再隐藏
    $("#panel_btn1").click(function(){
        var selected = $("#panel_btn1").prop("checked")
        if(selected){
           // console.log($("#hidden_div1").prop("style").display);
            $("#form_recognizee").form("clear")
            $("#hidden_div1").prop("style").display ="none";
        }else {
            $("#hidden_div1").prop("style").display ="block";
        }
    })
    //同车主按钮  选中后将车主信息填写框 先清除该表单的数据 再隐藏
    $("#panel_btn2").click(function(){
        var selected = $("#panel_btn2").prop("checked")
        if(selected){
            // console.log($("#hidden_div1").prop("style").display);
            $("#form_carOwner").form("clear")
            $("#hidden_div2").prop("style").display ="none";
        }else {
            $("#hidden_div2").prop("style").display ="block";
        }
    })
    //查询客户信息是的弹窗
    $("#clientinfo_dialog").dialog({
        title: 'My Dialog',
        width: 1200,
        height: 600,
        closed: true,
        /*href: 'client',*/
        /*content: '<iframe src="/orderBill/searchcllientinfo" width="100%" height="99%" frameborder="no"></iframe>',*/
        buttons:'#dialog_buttons'

    })
    //查询车辆信息是的弹窗
    $("#carinfo_dialog").dialog({
        title: 'My Dialog',
        width: 1200,
        height: 620,
        closed: true,
        /*href: 'client',*/
        /*content: '<iframe src="/orderBill/searchcllientinfo" width="100%" height="99%" frameborder="no"></iframe>',*/
        buttons:'#carInfo_dialog_buttons'

    })
    //客户信息查询弹窗中的表格-------------------------------------------------
    $(".client_datagrid").datagrid({
        url:'/sourceClient/list',
        fit:true,
        fitColumns:true,
        toolbar:'#datagrid_toolar',
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:'clientType',title:'客户类型',width:100},
            {field:'username',title:'账号名',width:60},
            {field:'realName',title:'客户姓名',width:100},
            {field:'tel',title:'手机号',width:100},
            {field:'qq',title:'QQ',width:100},
            {field:'email',title:'邮箱',width:100},
            {field:'address',title:'地址',width:100},
            {field:'idCardType',title:'证件类型',width:100},
            {field:'idCardNumber',title:'证件号码',width:100},
            {field:'job',title:'职业',width:100},
            {field:'clientNumber',title:'客户代码',width:100},
            {field:'clientLevel',title:'客户级别',width:100}
        ]]
    });
    //车辆信息查询弹窗中的表格-------------------------------------------------
    $("#carinfo_datagrid").datagrid({
        url:'/car/list',
        fit:true,
        fitColumns:true,
        toolbar:'#carInfo_datagrid_toolar',
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:'sn',title:'车型编码',width:60},
            {field:'carType',title:'车型分类',width:100},
            {field:'basesNumber',title:'核定载人',width:100},
            {field:'airRelease',title:'排气量',width:100},
            {field:'seatNumber',title:'座位数',width:100},
            {field:'rateDuality',title:'额定载质量(KG)',width:100},
            {field:'mainChassis',title:'车架号',width:100},
            {field:'engineNumber',title:'发动机号',width:100},
            {field:'newCarPrice',title:'新车购置价(元)',width:100},
            {field:'importation',title:'国产/进口',width:100},
            {field:'power',title:'功率',width:100}
        ]]
    });
    $("#dialog_checkApply").dialog({
        title: '批改申请说明',
        width: 330,
        height: 230,
        buttons:'#dialog_checkApplybtn',
        closed: true
    });

});
//************************************************以下都是自定义的方法****************************************************

//客户信息查询按钮  弹出客户信息页面
function client_search1(){
    $("#clientinfo_dialog").dialog("open")
}
//被保人查询按钮  弹出客户信息页面
function recognizee_search(){

    $("#clientinfo_dialog").dialog("open")
}
//车主信息查询按钮  弹出客户信息页面
function carOwner_search(){
    $("#clientinfo_dialog").dialog("open")
}
//车辆信息查询按钮  弹出车辆信息页面
function carInfo_search(){
    $("#carinfo_dialog").dialog("open")
}
//投保申请人信息确认  同时查询数据库 如果该客户存在就不保存 不存在就保存
function form_applyClient_check(){
    //先获取申请保单的id
    var orderBillId = $("[id=id]").val();
    //直接提交表单
    $("#form_applyClient").form("submit",{
        queryParams:{orderBillId:orderBillId},
        url:'/sourceClient/clientClick',
        success:function(data){
            data = $.parseJSON(data);
            if (data.success){
                $.messager.alert("温馨提示",data.msg,"info")
            }else {
                $.messager.alert("温馨提示",data.msg,"error")
            }
        }
    })

}
//投保申请人信息重置
function form_applyClient_reset(){
    $("#form_applyClient").form("reset")
}
//被保人信息确认  同时查询数据库 如果该客户存在就不保存 不存在就保存
/*function form_recognizee_check(){
    //先获取申请保单的id
    var orderBillId = $("[id=id]").val();
    //直接提交表单
    $("#form_recognizee").form("submit",{
        queryParams:{orderBillId:orderBillId},
        url:'/sourceClient/clientClick',
        success:function(data){
            data = $.parseJSON(data);
            if (data.success){
                $.messager.alert("温馨提示",data.msg,"info")
            }else {
                $.messager.alert("温馨提示",data.msg,"error")
            }
        }
    })

}*/
//======================================================================
//被保人信息页的页脚的按钮
function form_recognizee_check(){
    //先获取申请保单的id
    var orderBillId = $("[id=id]").val();
    var applyIdCard = $("#idCardNumber01").val();
    if (applyIdCard){
        //直接提交表单
        $("#form_recognizee").form("submit",{
            queryParams:{orderBillId:orderBillId},
            url:'/sourceClient/clientClick',
            success:function(data){
                data = $.parseJSON(data);
                if (data.success){
                    $.messager.alert("温馨提示",data.msg,"info")
                }else {
                    $.messager.alert("温馨提示",data.msg,"error")
                }
            }
        })
    }else {
        $.messager.alert("温馨提示","请先确认投保人信息","info")
    }

}
//被保人信息重置
function form_recognizee_reset(){
    $("#form_recognizee").form("reset")
}
//=======================================================================
//车主信息页的页脚的按钮
function form_carOwner_check(){
    //先获取申请保单的id
    var orderBillId = $("[id=id]").val();
    var applyIdCard = $("#idCardNumber02").val();
    if (applyIdCard){
        //直接提交表单
        $("#form_carOwner").form("submit",{
            queryParams:{orderBillId:orderBillId},
            url:'/sourceClient/clientClick',
            success:function(data){
                data = $.parseJSON(data);
                if (data.success){
                    $.messager.alert("温馨提示",data.msg,"info")
                }else {
                    $.messager.alert("温馨提示",data.msg,"error")
                }
            }
        })
    }else {
        $.messager.alert("温馨提示","请先确认被保人信息","info")
    }

}
//车主人信息重置
function form_carOwner_reset(){
    $("#form_carOwner").form("reset")
}
//======================================================================
//客户查找弹窗的确认按钮 点击确认 将选择的信息回显
function dialog_ok(){
    var selectedRow = $(".client_datagrid").datagrid("getSelected");
    if(selectedRow){
        //捕捉投保人身份证是否填写
        var applyIdCard = $("#idCardNumber01").val();
        console.log(applyIdCard);
        if (applyIdCard){
            //捕捉被保人身份证是否填写
            var applyIdCard2 = $("#idCardNumber02").val();
            if(applyIdCard2){
                $("#form_carOwner").form("load",selectedRow)
                $("#clientinfo_dialog").dialog("close")
            }else {
                $("#form_recognizee").form("load",selectedRow)
                $("#clientinfo_dialog").dialog("close")
            }

        }else {
            $("#form_applyClient").form("load",selectedRow)
            $("#clientinfo_dialog").dialog("close")
        }

    }else {
        $.messager.alert("温馨提示","请选择客户信息","info")
    }
}
//客户查找弹窗的返回按钮 点击返回 将关闭窗口
function dialog_cancel(){
    $("#clientinfo_dialog").dialog("close")
}
//车辆信息查找弹窗的确认按钮 点击确认 将选择的信息回显
function carInfo_dialog_ok(){
    var selectedRow = $("#carinfo_datagrid").datagrid("getSelected");
    console.log(selectedRow);
    if(selectedRow){
        $("#form_clientCarInfo").form("load",selectedRow)
        $("#carinfo_dialog").dialog("close")
    }else {
        $.messager.alert("温馨提示","请选择客户信息","info")
    }
}
//客户查找弹窗的返回按钮 点击返回 将关闭窗口
function carInfo_dialog_cancel(){
    $("#carinfo_dialog").dialog("close")
}
//申请批改按钮
function check_apply(){
    $("#dialog_checkApply").dialog("open")

}
//信息填写完成确认
function checkApply_ok(){
    var text = $("#applyText").textbox("getText");
    var orderBillId = $("[id=id]").val();
    $.messager.confirm("温馨提示","是否确认提交批改",function(ok){
        if(ok){
            $.post("/orderBill/checkApply",{orderId:orderBillId,remark:text},function(data){
                if (data.success){
                    $("#dialog_checkApply").dialog("close")
                    $.messager.alert("温馨提示",data.msg,"info")
                }else {
                    $.messager.alert("温馨提示",data.msg,"error")
                }
            },"json")
        }
    })

}
//投保人信息填写完成后进行客户信息确认 将信息保存到数据库中
function client_submit(){
    var orderBillId = $("[id=id]").val();
    var applyIdCard1 = $("#idCardNumber01").val();
    var applyIdCard2 = $("#idCardNumber02").val();
    var applyIdCard3 = $("#idCardNumber03").val();
    var accountInfoId = $("#accountInfoId").val();
    var clientCarInfoId = $("#clientCarInfoId").val();
    var organizationId = $("#organization_id").combobox("getValue");
    var productId = $("#product_id").combobox("getValue");
    var modelType = $("#modelType").val();
    var carNumber = $("#carNumber").val();
    console.log($("#id").val()+"传过来的id");
    //获取页面中的日期值并进行格式化
    var applyTime_fullYear = new Date($("#applyTime").datebox("getValue")).getFullYear();
    var applyTime_month = new Date($("#applyTime").datebox("getValue")).getMonth();
    var applyTime_day = new Date($("#applyTime").datebox("getValue")).getDate();
    var applyTime_date = applyTime_fullYear +"/"+ (applyTime_month+1)+"/"+applyTime_day;

    var beginTime_fullYear = new Date($("#beginTime").datebox("getValue")).getFullYear();
    var beginTime_month = new Date($("#beginTime").datebox("getValue")).getMonth();
    var beginTime_day = new Date($("#beginTime").datebox("getValue")).getDate();
    var beginTime_date = beginTime_fullYear +"/"+ (beginTime_month+1)+"/"+beginTime_day;

    var endTime_fullYear = new Date($("#endTime").datebox("getValue")).getFullYear();
    var endTime_month = new Date($("#endTime").datebox("getValue")).getMonth();
    var endTime_day = new Date($("#endTime").datebox("getValue")).getDate();
    var endTime_date = endTime_fullYear +"/"+ (endTime_month+1)+"/"+endTime_day;

    var registerTime_fullYear = new Date($("#registerTime").datebox("getValue")).getFullYear();
    var registerTime_month = new Date($("#registerTime").datebox("getValue")).getMonth();
    var registerTime_day = new Date($("#registerTime").datebox("getValue")).getDate();
    var registerTime_date = registerTime_fullYear +"/"+ (registerTime_month+1)+"/"+registerTime_day;

    var transferTime_fullYear = new Date($("#transferTime").datebox("getValue")).getFullYear();
    var transferTime_month = new Date($("#transferTime").datebox("getValue")).getMonth();
    var transferTime_day = new Date($("#transferTime").datebox("getValue")).getDate();
    var transferTime_date = transferTime_fullYear +"/"+ (transferTime_month+1)+"/"+transferTime_day;
    //提交时间信息和基本信息
    $("#form_timeandbaseinfo").form("submit",{
        queryParams:{applyClient:applyIdCard1,
            recognizee:applyIdCard2,
            carOwner:applyIdCard3,
            organizationId:organizationId,
            productId:productId,
            modelType:modelType,
            carNumber:carNumber,
            applyTime:applyTime_date,
            beginTime:beginTime_date,
            endTime:endTime_date
        },
        url:'/orderBill/updateTimeAndBaseInfo'
    })
    //提交车辆信息和车辆附加信息
    $("#form_clientCarInfo").form("submit",{
        queryParams:{id:clientCarInfoId,
            orderBillId:orderBillId,
            registerTime:registerTime_date,
            transferTime:transferTime_date},
        url:'/clientCarInfo/updateClientCarInfo'
    })
    //提交账户信息信息和车辆附加信息
    $("#form_accountInfo").form("submit",{
        queryParams:{id:accountInfoId,applyClient:applyIdCard1},
        url:'/accountInfo/updateAccountInfo'
    })
    /* $("#form_applyClient").form("submit",{
     url:'employee',
     onSubmit: function(param){
     param.recognizee = 'value1'; //捕捉被保人身份证号码
     param.carOwner = 'value2';//捕捉车主身份证号码
     }
     })*/
}

